using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using SumerCoreDevOps.Application.NodeLogs.Queries;
using SumerCoreDevOps.Shared.Models;

namespace SumerCoreDevOps.ControlApi.Controllers;

/// <summary>
/// 节点操作日志查询接口
/// </summary>
[ApiController]
[Route("api/[controller]")]
[Authorize]
public class NodeLogController : ControllerBase
{
    private readonly IMediator _mediator;
    private readonly ILogger<NodeLogController> _logger;

    public NodeLogController(IMediator mediator, ILogger<NodeLogController> logger)
    {
        _mediator = mediator;
        _logger = logger;
    }

    /// <summary>
    /// 根据节点 ID 获取操作日志
    /// </summary>
    /// <param name="nodeId">节点ID</param>
    /// <param name="limit">返回记录数量限制，默认100条</param>
    /// <param name="offset">偏移量，默认0</param>
    /// <returns>返回指定节点的操作日志列表,每条日志包含日志级别、操作类型、消息内容、时间戳等信息;支持分页查询;数据封装在ApiResponse对象中</returns>
    [HttpGet("node/{nodeId}")]
    public async Task<IActionResult> GetLogsByNodeId(
        string nodeId,
        [FromQuery] int limit = 100,
        [FromQuery] int offset = 0)
    {
        var logs = await _mediator.Send(new GetLogsByNodeIdQuery(nodeId, limit, offset));
        return Ok(ApiResponse.Ok(logs));
    }

    /// <summary>
    /// 根据任务 ID 获取操作日志
    /// </summary>
    /// <param name="taskId">任务ID</param>
    /// <returns>返回指定任务的所有相关操作日志列表,包括任务创建、执行、完成、失败等各阶段的日志记录;数据封装在ApiResponse对象中</returns>
    [HttpGet("task/{taskId}")]
    public async Task<IActionResult> GetLogsByTaskId(string taskId)
    {
        var logs = await _mediator.Send(new GetLogsByTaskIdQuery(taskId));
        return Ok(ApiResponse.Ok(logs));
    }

    /// <summary>
    /// 根据流水线实例 ID 获取操作日志
    /// </summary>
    /// <param name="pipelineInstanceId">流水线实例ID</param>
    /// <returns>返回指定流水线实例的所有相关操作日志列表,包括流水线启动、任务分配、执行进度、完成状态等各环节的日志记录;数据封装在ApiResponse对象中</returns>
    [HttpGet("pipeline/{pipelineInstanceId}")]
    public async Task<IActionResult> GetLogsByPipelineInstanceId(long pipelineInstanceId)
    {
        var logs = await _mediator.Send(new GetLogsByPipelineInstanceIdQuery(pipelineInstanceId));
        return Ok(ApiResponse.Ok(logs));
    }

    /// <summary>
    /// 获取最近的操作日志
    /// </summary>
    /// <param name="limit">返回记录数量限制，默认50条</param>
    /// <param name="logLevel">日志级别过滤（可选）</param>
    /// <param name="operationType">操作类型过滤（可选）</param>
    /// <returns>返回最近的操作日志列表,按时间倒序排列;可通过日志级别和操作类型进行过滤;数据封装在ApiResponse对象中</returns>
    [HttpGet("recent")]
    public async Task<IActionResult> GetRecentLogs(
        [FromQuery] int limit = 50,
        [FromQuery] string? logLevel = null,
        [FromQuery] string? operationType = null)
    {
        var logs = await _mediator.Send(new GetRecentLogsQuery(limit, logLevel, operationType));
        return Ok(ApiResponse.Ok(logs));
    }

    /// <summary>
    /// 获取错误日志
    /// </summary>
    /// <param name="startDate">开始日期（可选）</param>
    /// <param name="endDate">结束日期（可选）</param>
    /// <param name="limit">返回记录数量限制，默认100条</param>
    /// <returns>返回指定时间范围内的所有错误级别日志列表,包含错误信息、堆栈跟踪、发生时间等详细内容;如果不指定时间范围则返回所有错误日志;数据封装在ApiResponse对象中</returns>
    [HttpGet("errors")]
    public async Task<IActionResult> GetErrorLogs(
        [FromQuery] DateTime? startDate = null,
        [FromQuery] DateTime? endDate = null,
        [FromQuery] int limit = 100)
    {
        var logs = await _mediator.Send(new GetErrorLogsQuery(startDate, endDate, limit));
        return Ok(ApiResponse.Ok(logs));
    }

    /// <summary>
    /// 获取日志统计信息
    /// </summary>
    /// <param name="startDate">开始日期（可选）</param>
    /// <param name="endDate">结束日期（可选）</param>
    /// <param name="nodeId">节点ID过滤（可选）</param>
    /// <returns>返回日志统计数据对象,包括各日志级别的数量分布、各操作类型的数量统计、错误率等信息;可按时间范围和节点进行筛选;数据封装在ApiResponse对象中</returns>
    [HttpGet("statistics")]
    public async Task<IActionResult> GetStatistics(
        [FromQuery] DateTime? startDate = null,
        [FromQuery] DateTime? endDate = null,
        [FromQuery] string? nodeId = null)
    {
        var stats = await _mediator.Send(new GetLogStatisticsQuery(startDate, endDate, nodeId));
        return Ok(ApiResponse.Ok(stats));
    }
}
